package org.zxp.onedoteight.datetest;

import org.junit.Test;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;

/**
 * @program: effectiveJava
 * @description:
 * @author: X-Pacific zhang
 * @create: 2020-03-12 17:10
 **/
public class DateTest {

    @Test
    public void testLocalDate() {
        LocalDate today = LocalDate.now();
        System.out.println("今天⽇期：" + today);
        System.out.println("现在是哪年:" + today.getYear());
        System.out.println("现在是哪⽉:" + today.getMonth());
        System.out.println("现在是哪⽉(数字):" + today.getMonthValue());
        System.out.println("现在是⼏号:" + today.getDayOfMonth());
        System.out.println("现在是周⼏:" + today.getDayOfWeek());

        LocalDate changeDate = today.plusYears(1);
        System.out.println("加后是哪年:" + changeDate.getYear());
        System.out.println("旧的是哪年:" + today.getYear());

        //⽇期⽐较
        System.out.println("isAfter: " + changeDate.isAfter(today));
        System.out.println("isAfter: " + changeDate.compareTo(today));

//getYear() int 获取当前⽇期的年份
//getMonth() Month 获取当前⽇期的⽉份对象
//getMonthValue() int 获取当前⽇期是第⼏⽉
//getDayOfWeek() DayOfWeek 表示该对象表示的⽇期是星期⼏
//getDayOfMonth() int 表示该对象表示的⽇期是这个⽉第⼏天
//getDayOfYear() int 表示该对象表示的⽇期是今年第⼏天
//withYear(int year) LocalDate 修改当前对象的年份
//withMonth(int month) LocalDate 修改当前对象的⽉份
//withDayOfMonth(int dayOfMonth) LocalDate 修改当前对象在当⽉的⽇期
//plusYears(long yearsToAdd) LocalDate 当前对象增加指定的年份数
//plusMonths(long monthsToAdd) LocalDate 当前对象增加指定的⽉份数
//plusWeeks(long weeksToAdd) LocalDate 当前对象增加指定的周数
//plusDays(long daysToAdd) LocalDate 当前对象增加指定的天数
//minusYears(long yearsToSubtract) LocalDate 当前对象减去指定的年数
//minusMonths(long monthsToSubtract) LocalDate 当前对象减去注定的⽉数
//minusWeeks(long weeksToSubtract) LocalDate 当前对象减去指定的周数
//minusDays(long daysToSubtract) LocalDate 当前对象减去指定的天数
//compareTo(ChronoLocalDate other) int ⽐较当前对象和other对象在时间上的⼤⼩，返回值如果为正，则当前对象时间较晚，
//isBefore(ChronoLocalDate other) boolean ⽐较当前对象⽇期是否在other对象⽇期之前
//isAfter(ChronoLocalDate other) boolean ⽐较当前对象⽇期是否在other对象⽇期之后
//isEqual(ChronoLocalDate other) boolean ⽐较两个⽇期对象是否相等

        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);
        System.out.println(localTime.getHour());

        LocalDateTime localDateTime = LocalDateTime.of(2020, 3, 12, 1, 2, 3);
        System.out.println(localDateTime);
    }

    @Test
    public void testFmt() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String ldtStr = dtf.format(ldt);
        System.out.println(ldtStr);
    }

    @Test
    public void testGetTime() {
        LocalDateTime localDateTime = LocalDateTime.of(2020, 3, 12, 1, 2, 3);
        System.out.println(localDateTime);
        LocalDate localDate = LocalDate.of(2020, 10, 1);
        System.out.println(localDate);
    }

    @Test
    public void testDuration() {
        LocalDateTime today = LocalDateTime.now();
        System.out.println(today);
        LocalDateTime changeDate = LocalDateTime.of(2020, 10, 1, 10, 40, 30);
        System.out.println(changeDate);
        Duration duration = Duration.between(today, changeDate);//第⼆个参数减第⼀个参数
        System.out.println(duration.toDays());//两个时间差的天数
        System.out.println(duration.toHours());//两个时间差的⼩时数
        System.out.println(duration.toMinutes());//两个时间差的分钟数
        System.out.println(duration.toMillis());//两个时间差的毫秒数
        System.out.println(duration.toNanos());//两个时间差的纳秒数
    }
}
